package demo2;

import java.util.ArrayList;
import java.util.List;

public class Text {
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> list = new ArrayList<>();
        char[] arrs = s.toCharArray();
        char[] arrp = p.toCharArray();
        int ns = arrs.length, np = arrp.length;
        int[] hashs = new int[126];
        int[] hashp = new int[126];
        for (int i = 0; i < np; i++) {
            hashp[arrp[i]]++;
        }
        for (int left = 0, right = 0, k = 0; right < ns; right++) {
            int len = right - left + 1;
            hashs[arrs[right]]++;
            if (hashs[arrs[right]] <= hashp[arrs[right]]) k++;
            if (len > np) {
                if (hashs[arrs[left]] <= hashp[arrs[left]]) k--;
                hashs[arrs[left++]]--;
            }
            if (k == np) list.add(left);
        }
        return list;
    }
}
